﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Data;

namespace LLKanWebService
{
    public class MySQLHepler
    {
        private static string connectionString = ConfigurationManager.ConnectionStrings["MySqlStr"].ConnectionString;
        protected MySqlConnection connection = new MySqlConnection(connectionString);

        public void openConn()
        {
            try
            {
                if (connection.State == ConnectionState.Closed)
                    connection.Open();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw new Exception(ex.Message);
            }
        }
        public void closeConn()
        {
            try
            {
                if (connection.State == ConnectionState.Open)
                    connection.Close();
            }
            catch (System.Data.SqlClient.SqlException ex)
            {
                throw new Exception(ex.Message);
            }
        }


        //public static DataSet Query(string SQLString)
        //{
        //    using (MySqlConnection connection = new MySqlConnection(connectionString))
        //    {
        //        DataSet dt = new DataSet();
        //        try
        //        {
        //            connection.Open();
        //            MySqlDataAdapter command = new MySqlDataAdapter(SQLString, connection);
        //            command.Fill(dt);
        //        }
        //        catch (System.Data.SqlClient.SqlException ex)
        //        {
        //            throw new Exception(ex.Message);
        //        }
        //        finally
        //        {
        //            connection.Close();
        //        }
        //        return dt;
        //    }
        //}
        ///// <summary>
        ///// 执行SQL语句，返回影响的记录数
        ///// </summary>
        ///// <param name="SQLString">SQL语句</param>
        ///// <returns>影响的记录数</returns>
        //public static int ExecuteSql(string SQLString)
        //{
        //    using (MySqlConnection connection = new MySqlConnection(connectionString))
        //    {
        //        using (MySqlCommand cmd = new MySqlCommand(SQLString, connection))
        //        {
        //            try
        //            {
        //                connection.Open();
        //                int rows = cmd.ExecuteNonQuery();
        //                return rows;
        //            }
        //            catch (System.Data.SqlClient.SqlException e)
        //            {
        //                connection.Close();
        //                throw e;
        //            }
        //            finally
        //            {
        //                cmd.Dispose();
        //                connection.Close();
        //            }
        //        }
        //    }
        //}
    }
}